package com.quocnv.mediascanner;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;

public class FileScanner
{
	public static FileScanner getInstance()
	{
		return new FileScanner();
	}

	public ArrayList<File> getAll(String path, FileFilter filter)
	{
		File root = new File(path);
		return scanFiles(root, filter);

	}

	private ArrayList<File> scanFiles(File root, FileFilter filter)
	{
		ArrayList<File> fis = new ArrayList<File>();
		File[] files = root.listFiles();

		for (File file : files)
		{
			if (file.isFile())
			{
				if (filter == null)
				{
					fis.add(file);
				}
				else if (filter.accept(file))
					fis.add(file);

			}
			else if (file.isDirectory())
			{
				fis.addAll(scanFiles(file, filter));
			}
		}

		return fis;
	}

	public ArrayList<File> getFiles(String path, FileFilter filter)
	{
		ArrayList<File> files = new ArrayList<File>();
		File file = new File(path);
		files.add(file.getParentFile());

		for (File f : file.listFiles())
		{
			if (f.isDirectory())
				files.add(f);

			else if (f.isFile())
			{
				if (filter == null)
				{
					files.add(f);
				}
				else if (filter.accept(f))
					files.add(f);
			}
		}
		return files;
	}
}
